﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;

namespace ch01
{
    public partial class FrmListStu : Form
    {
        public FrmListStu()
        {
            InitializeComponent();
        }

        private void FrmListStu_Load(object sender, EventArgs e)
        {
            BindCombobox();
            BindListView();
        } 
        public void BindCombobox()
        {
           string sql = "select * from grade";
            DataTable dt=DBHelper.SelectTable(sql);
            DataRow dr = dt.NewRow();
            dr["no"]=0;
            dr["gradename"]="全部";
            dt.Rows.InsertAt(dr,0);
            this.comboBox1.DisplayMember = "gradename";
            this.comboBox1.ValueMember = "no";
            this.comboBox1.DataSource = dt;
        }
        public void BindListView()
        {
            this.listView1.Items.Clear();

            string sql = string.Format("select s.*,g.GradeName from Student s,Grade g where s.GradeId=g.NO and 1=1");
            string name = this.txt_name.Text;
            if (!string.IsNullOrEmpty(name))
            {
                sql += string.Format(" and name like '%{0}%'", name);
            }
            int gradeId = Convert.ToInt32(this.comboBox1.SelectedValue);
            if (gradeId != 0)
            {
                sql += string.Format(" and s.GradeId={0}", gradeId);
            }

            SqlDataReader reader = DBHelper.SelectMore(sql);
            while (reader.Read())
            {
                ListViewItem item = new ListViewItem(reader["sutno"].ToString());
               
                item.SubItems.Add(reader["pwd"].ToString());
                item.SubItems.Add(reader["name"].ToString());
                item.SubItems.Add(reader["sex"].ToString());
                item.SubItems.Add(reader["GradeName"].ToString());
                item.SubItems.Add(reader["PHone"].ToString());
                item.SubItems.Add(reader["address"].ToString());
                item.SubItems.Add(reader["bomdate"].ToString());
                item.SubItems.Add(reader["email"].ToString());

                listView1.Items.Add(item);
                
            }
            reader.Close();        
        }

        private void button1_Click(object sender, EventArgs e)
        {

            
           BindListView();
           if (listView1.Items.Count <= 0)
           {
               MessageBox.Show("没有查询到数据！");
               return;
           }
 
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.listView1.SelectedItems.Count<=0)
            {
                MessageBox.Show("至少选中一条数据！");
                return;
            }
            DialogResult dr=MessageBox.Show("您真的要删除吗？","提示信息！",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
               ListViewItem item=this.listView1.SelectedItems[0];
               int sutNo = Convert.ToInt16(item.Text);
               string sql = "delete from Student where sutno=" + sutNo;
               int result = DBHelper.DataUpdate(sql);
               if (result > 0)
               {
                   MessageBox.Show("删除成功！");
                   BindListView();//更新数据
               }
               else
               {
                   MessageBox.Show("删除失败！");
               }
            }
        }
        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.listView1.SelectedItems.Count<=0)
            {
                MessageBox.Show("请至少选择一条数据！");
                return;
            }
            ListViewItem item=this.listView1.SelectedItems[0];
            int sutNo = Convert.ToInt32(item.Text);
            FrmUpdateSutInfo fu = new FrmUpdateSutInfo();
            fu.sutNo = sutNo;
            fu.ShowDialog();
            BindListView();//更新数据
        }

    }
}
